package com.app.utils
{
	import flash.display.Stage;
	import flash.events.Event;
	import flash.filters.GlowFilter;
	import flash.system.System;
	import flash.text.TextField;
	import flash.utils.getTimer;
	
	/**
	 * FPS 
	 * @author Billows
	 * 
	 */	
	public class FPS
	{
		private static const _loopConut:int=30;
		private static var _runCount:int=0;
		private static var _newTime:int=0;
		
		private static var _txt:TextField;
		
		/**
		 *  
		 * @param stage 容器
		 * 
		 */		
		public static function Start(stage:Stage=null):void
		{
			if(_txt==null)
			{
				_txt=new TextField();
				_txt.selectable = false;
				_txt.autoSize="left";
				
				var glowFilter:GlowFilter = new GlowFilter(0xFFFFFF,1,3,3,8,3);
				var filtersArray:Array = new Array(glowFilter);
				_txt.filters=filtersArray;
				stage.addChild(_txt);
				_txt.y=3;
				_txt.x=stage.stageWidth-60;
			}
			
			stage.addEventListener(Event.ENTER_FRAME, FPSEvent);
		}
		
		private static function FPSEvent(event:Event):void
		{
			_runCount++;
			if(_runCount>=_loopConut)
			{
				_runCount=0;
				_txt.text = "FPS: " + Number(1000 * _loopConut / (getTimer() - _newTime)).toFixed(1)
					+("\r\n"+"MB: "+Number(System.totalMemory/(1024*1024)).toFixed(2));
				_newTime = getTimer(); 
			}
		}
	}
}